package top.heue.text.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import top.heue.text.R;
import top.heue.text.bean.MainListBean;

public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.ViewHolder> {

    private final List<MainListBean> list;

    public MainListAdapter(List<MainListBean> list) {
        this.list = list;
    }

    public void addItem(MainListBean mainListBean) {
        list.add(0, mainListBean);
        notifyItemInserted(0);
    }

    public void deleteItem(MainListBean mainListBean) {
        int position = list.indexOf(mainListBean);
        list.remove(mainListBean);
        if(position >= 0)
            notifyItemRemoved(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public MainListBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public MainListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainListAdapter.ViewHolder holder, int position) {
        holder.txt_title.setText(list.get(position).title);
        holder.txt_some.setText(list.get(position).some);
        //holder.txt_info.setText(list.get(position).info);
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(v -> {
                int pos = holder.getAdapterPosition();
                onItemClickListener.onItemClick(holder.itemView, pos);
            });
        }
        if(onItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(v -> {
                int pos = holder.getAdapterPosition();
                onItemLongClickListener.onItemLongClick(holder.itemView, pos);
                return true;
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    private OnItemLongClickListener onItemLongClickListener;

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView txt_title;
        public TextView txt_some;
        //public TextView txt_info;

        public ViewHolder(View itemView) {
            super(itemView);
            txt_title = itemView.findViewById(R.id.item_main_txt_title);
            txt_some = itemView.findViewById(R.id.item_main_txt_some);
            //txt_info = itemView.findViewById(R.id.item_main_txt_info);
        }
    }
}
